﻿#include "FormTileDEM.h"
#include "ui_FormTileDEM.h"
#include <QFileDialog>
#include "ComputeLevels.h"
#include "TileService.h"
#include "qdebug.h"
FormTileDEM::FormTileDEM(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormTileDEM)
{
    ui->setupUi(this);
    connect(ui->pushButtonAddFile,SIGNAL(clicked(bool)),this,SLOT(slotAddFile()));
    connect(ui->pushButtonGetLV,SIGNAL(clicked(bool)),this,SLOT(slotGetLevels()));
    connect(ui->pushButtonOK,SIGNAL(clicked(bool)),this,SLOT(slotOK()));
    connect(ui->pushButtonCancel,SIGNAL(clicked(bool)),this,SLOT(slotCancel()));
    connect(ui->pushButtonSltOutPath,SIGNAL(clicked(bool)),this,SLOT(slotSltOutPath()));
    connect(ui->pushButtonClearFiles,SIGNAL(clicked(bool)),this,SLOT(slotClearFiles()));
    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
}

FormTileDEM::~FormTileDEM()
{
    delete ui;
}

TileOption FormTileDEM::_getTileOption()
{
    TileOption option;
    option.tilesize = ui->lineEditTileSize->text().toInt();
    option.tile_lon_0= option.extent.width() / (ui->lineEditlv1x->text().toInt());
    option.tile_lat_0= option.extent.height() / (ui->lineEditlv1y->text().toInt());
    return option;
}

QString FormTileDEM::_getInputFile(int i)
{
    if(ui->tableWidget->rowCount() <= i) return "";
    return ui->tableWidget->item(i, 0)->text();
}

void FormTileDEM::slotAddFile()
{
    QString str = QFileDialog::getOpenFileName(nullptr, QStringLiteral("选择"));
    if (str.length() <= 0) {
        return;
    }

    int rowcount = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(rowcount+1);
    QTableWidgetItem *newItem = new QTableWidgetItem(str);
    ui->tableWidget->setItem(rowcount, 0, newItem);
}

void FormTileDEM::slotClearFiles()
{
    ui->tableWidget->setRowCount(0);
}

void FormTileDEM::slotGetLevels()
{
        QString path =_getInputFile(0);
    if(path.length() <= 0) return;
    TileOption op = this->_getTileOption();



    ComputeLevels cl;
    int min=0,max=0;
    cl.computeLevels(&op,path,min,max);

    ui->lineEditMinLV->setText(QString::number(min));
    ui->lineEdit_MaxLV->setText(QString::number(max));
}

void FormTileDEM::slotOK()
{
    QString str = ui->tableWidget->item(0,0)->text();
    if(str.length() <= 1) return;
    TileServiceOption option;
    option.tileOption = _getTileOption();
    option.taskType = TileServiceOption::DEM;
    option.tileType = TileServiceOption::DEM_TERRAIN;
    option.ext ="terrain";
    if(ui->comboBoxType->currentText() == QString("bil")){
        option.tileType = TileServiceOption::DEM_BIL;
        option.ext ="bil";
    }
    option.ispak =ui->radioButtonpak->isChecked();

    option.inputFiles.append(str);
    option.level0= ui->lineEditMinLV->text().toInt();
    option.level1= ui->lineEdit_MaxLV->text().toInt();
    if(option.level0 > option.level1) return;

    option.outDir = ui->lineEditOutPath->text();
    if(option.outDir.length() <= 1)return;

    if(_tileService ==0){
        _tileService = new TileService();
    }
    if(_tileService->isRunning()) return;
    _tileService->handle(option);

}

void FormTileDEM::slotCancel()
{
    if(_tileService){
        _tileService->terminate();
    }
}

void FormTileDEM::slotSltOutPath()
{
    QString file_dir = QFileDialog::getExistingDirectory(this,
                   "选择保存路径",
                   "./",
                    QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if( file_dir.isEmpty() )
    {
        return;
    }
    else
    {
        ui->lineEditOutPath->setText( file_dir );
    }
}
